package cn.singno.bob.jpa.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;

import cn.singno.bob.jpa.dao.UserDao;
import cn.singno.bob.jpa.entity.Student;
import cn.singno.bob.jpa.entity.User;

@Service
public class UserServiceImpl implements UserService{

	@Autowired
	private UserDao userDao;
	@Autowired
	private StudentService studentService;
	
	public User save(User user) {
		userDao.save(user);
		/*if(1 == 1){
			throw new RuntimeException();
		}*/
		userDao.findOne("123");					//本类中如果只会拦截器主方法。以主方法为事务类型
		studentService.findcutM(new Student());
		return user;
	}

	public User findById(String id) {
		User user = new User();
		user.setId("1231123aba" );
		user.setAge(12);
		user.setName("haha");
		save(user);
		return userDao.findOne(id);
	}

	public int update(String name, String id) {
		return userDao.update(name, id);
	}

	public void delete(String id) {
		userDao.delete(id);
	}

	public List<User> findAll() {
		return userDao.findAll();
		
	}

	public Page<User> find(int pageNow, int pageSize) {
		return userDao.findAll(builderPageable(pageNow, pageSize));
	}

	private Pageable builderPageable(int pageNow, int pageSize){
		Pageable page = new PageRequest(pageNow, pageSize);
		return page;
	}

	public long count() {
		return userDao.count();
	}

	public List<User> findByName(String name) {
		return userDao.findByName(name);
	}

	public Page<User> findByName(String name, int pageNow, int pageSize) {
		return userDao.findByName(name, new PageRequest(pageNow, pageSize));
	}

	public List<User> find2ByName(String name) {
		return userDao.find2ByName(name);
	}

	public User leftJoin(String userId) {
		return userDao.leftJoin(userId);
	}

	public Page<User> leftJoin(String userId, int pageNow, int pageSize) {
		//return userDao.leftJoin(userId, new PageRequest(pageNow, pageSize));
		return null;
		
	}

	public Map<String, Object> findMap(String userId) {
		return userDao.findMap(userId);
	}

	public List<Map<String, Object>> findMap3(String userName) {
		
		return userDao.findMap3(userName);
	}

	public Page<Map<String, Object>> findMap4(String userName, int pageNow, int pageSize) {
		return userDao.findMap4(userName, new PageRequest(pageNow, pageSize, new Sort(Direction.DESC, "age")));
	}

	public Page<User> findNativAll() {
		/*return userDao.findNative("SELECT o.* From User o left join Student s on o.id = s.userId where s.userId = ? ", 
				"SELECT count(o.id) From User o left join Student s on o.id = s.userId where s.userId = ?", "12311230", 1, 2);*/
		return null;
	}

	public User findBy(String id, String name) {
		return userDao.findBy(id, name);
	}

	@Override
	public List<User> cusFind(String name) {
		return null;
	}

	@Override
	public List<User> bobExp(Student student) {
		return null;
	}
	
}
